package org.posta.mail.store;

import java.io.IOException;
import java.util.Date;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.posta.data.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MailMessage
{
    private static Logger     log    = LoggerFactory.getLogger(MailMessage.class);
    
    private final User        user;
    
    private final MimeMessage message;
    
    private boolean           isRead = false;
    
    private final Date        dateReceived;
    
    public MailMessage(User user, MimeMessage message)
    {
        this.user = user;
        this.message = message;
        this.dateReceived = new Date();
    }
    
    public User getUser()
    {
        return user;
    }
    
    public MimeMessage getMessage()
    {
        return message;
    }
    
    public boolean isRead()
    {
        return isRead;
    }
    
    public void setRead(boolean isRead)
    {
        this.isRead = isRead;
    }
    
    public Date getDateReceived()
    {
        return dateReceived;
    }
    
    // Delegate methods
    
    public Address[] getAllRecipients()
    {
        try
        {
            return message.getAllRecipients();
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    public Object getContent()
    {
        try
        {
            return message.getContent();
        } catch (IOException e)
        {
            log.error("Error reading message data", e);
            return null;
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    public String getEncoding()
    {
        try
        {
            return message.getEncoding();
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    public Address getFrom()
    {
        try
        {
            return message.getFrom()[0];
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    public String getMessageID()
    {
        try
        {
            return message.getMessageID();
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    public int getSize()
    {
        try
        {
            return message.getSize();
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return 0;
        }
    }
    
    public String getSubject()
    {
        try
        {
            return message.getSubject();
        } catch (MessagingException e)
        {
            log.error("Error reading message data", e);
            return null;
        }
    }
    
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(user).append(" | ").append(getFrom()).append(" | ").append(
                getSubject()).append(" | ").append(dateReceived);
        return sb.toString();
    }
}
